package base.thread.waitNotify;

import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 当线程达到指定数目后会被唤醒
 */
public class CyclicBarrierDemo {

    private CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
    //当线程达到指定数目后会被唤醒


    public void startThread() throws BrokenBarrierException, InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name + "  reading ");

        cyclicBarrier.await();
        System.out.println(name + "  已经启动...."+new Date().getTime());


    }

    public static void main(String[] args) {

        CyclicBarrierDemo demo = new CyclicBarrierDemo();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    demo.startThread();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    demo.startThread();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程2").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    demo.startThread();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程3").start();

    }
}

//线程1  reading
//线程2  reading
//线程3  reading
//线程3  已经启动....1568113421995
//线程2  已经启动....1568113421996
//线程1  已经启动....1568113421996
